const path = require('path')
const webpack = require('webpack')
const { merge } = require('webpack-merge')
const webpackBaseConfig = require('./webpack.base.config')
const { srcPath, publicPath, buildPath, iconPath } = require('./path')

// css 压缩
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
// 每次打包前清除旧的build文件夹
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
// 复制public中的文件到打包的最终文件夹中
const CopyPlugin = require("copy-webpack-plugin")
// js 压缩
const TerserPlugin = require("terser-webpack-plugin")

const { version } = require("../package.json")

const bannerDescription = `
/**
 *  React Core Semi Framework
 *  version: ${version}
 *  time: ${new Date().toLocaleDateString()}
 */
`

const webpackDevConfig = {
  mode: 'production',
  devtool: 'nosources-source-map',
  output: {
    // 打包后的public path，即基础路径
    publicPath: process.env.PUBLIC_URL || '/',
  },
  optimization: {
    minimize: true,
    minimizer: [
      new TerserPlugin({
        parallel: true, // 多线程并行构建
        terserOptions: {
          // https://github.com/terser/terser#minify-options
          compress: {
            warnings: false, // 删除无用代码时是否给出警告
            // drop_console: true, // 删除所有的console.*
            drop_debugger: true, // 删除所有的debugger
            pure_funcs: ["console.log"], // 删除所有的console.log
          },
        },
      }),
      new CssMinimizerPlugin(),
    ],
    splitChunks: {
      chunks: "all",
    },
  },
  plugins: [
    new CleanWebpackPlugin(),
    new CopyPlugin({
      patterns: [
        {
          from: publicPath,
          to: buildPath,
          globOptions: {
            ignore: ["**/favicon.png", "**/index.html"],
          },
          noErrorOnMissing: true,
        },
      ],
    }),
    // 打包后 头部注释
    new webpack.BannerPlugin({
      raw: true,
      banner: bannerDescription,
    }),
  ]
}

module.exports = merge(webpackBaseConfig, webpackDevConfig)